package reading.common.enums;

/**
 * Create by txw on
 * Description:
 * DataSourceMap:
 *
 * @author txw
 * @since 2017/11/8
 **/
public class CodeEnumUtil {
    /**
     * @param enumClass
     * @param code
     * @param <E>
     * @return
     */
    public static <E extends Enum<?> & IBaseEnum> E codeOf(Class<E> enumClass, int code) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getValue() == code) {
                return e;
            }
        }
        return null;
    }

    public static <E extends Enum<?> & IBaseEnum> E stringOf(Class<E> enumClass, String name) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getName().equals(name)) {
                return e;
            }
        }
        return null;
    }
}
